package gui;

import main.Session;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.wb.swt.SWTResourceManager;

import Utils.Concurrent.ThreadTasks.LoaderTask;
import Utils.UI.UIUpdater;

public class LoaderGif {
	
	private Canvas canvas;
	private ImageLoader loader = new ImageLoader();
	private GC gc;
	private LoaderTask loaderTask;
	private Image image;
	private int loaderType = 0; // 1 small, 2 small transparent, 3 big transparent 4 big
	
	
	
	
	public LoaderGif(Canvas canvas, int loaderType) {
		this.canvas = canvas;
		this.loaderType = loaderType;
		prepareLoader();
		
		
	}
	
	
	private void prepareLoader() {
		if(loaderType == 1)
			loader.load(GUI.class.getResourceAsStream("/res/small loader.gif"));
		else if(loaderType == 2)
			loader.load(GUI.class.getResourceAsStream("/res/small loader trans.gif"));
		else if(loaderType == 3)
			loader.load(GUI.class.getResourceAsStream("/res/big loader trans.gif"));
		else
			loader.load(GUI.class.getResourceAsStream("/res/big loader.gif"));
		image = new Image(Session.gui.getDisplay(), loader.data[0]);
		gc = new GC(image);
		
		canvas.addPaintListener(new PaintListener() {
			@Override
			public void paintControl(PaintEvent event) {
				event.gc.drawImage(image, 0, 0);	
			}
		});
		
		loaderTask = new LoaderTask(Session.gui.getDisplay(), loader,canvas,gc);
		
		
	}
	
	public void hideLoader(){
		UIUpdater.setWidgetVisibile(getCanvas(), false);
	}
	
	public void showLoader(){
		UIUpdater.setWidgetVisibile(getCanvas(), true);
	}


	public synchronized Canvas getCanvas() {
		return canvas;
	}
	public synchronized void setCanvas(Canvas canvas) {
		this.canvas = canvas;
	}
	public synchronized ImageLoader getLoader() {
		return loader;
	}
	public synchronized void setLoader(ImageLoader loader) {
		this.loader = loader;
	}
	public synchronized GC getGc() {
		return gc;
	}
	public synchronized void setGc(GC gc) {
		this.gc = gc;
	}
	public synchronized LoaderTask getLoaderTask() {
		return loaderTask;
	}
	public synchronized void setLoaderTask(LoaderTask loaderTask) {
		this.loaderTask = loaderTask;
	}
	
	

}
